package net.vinote.sosa.core.module.security;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.logging.Level;

import net.vinote.smart.socket.lang.StringUtils;
import net.vinote.sosa.core.module.log.RunLogger;

/**
 * 对系统添加安全加固,防止被未注册的用户使用
 *
 * @author Seer
 * @version PlatformSecurity.java, v 0.1 2015年3月11日 上午10:06:15 Seer Exp.
 */
public class SecurityUtils {

	private static final String key = "!Q2w3e4r5t6y7u8i";

	private SecurityUtils() {
	}

	public static String generateRegistrationCode(byte[] mac, String... cfgStr) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (String cfg : cfgStr) {
			sb.append(cfg).append(":");
		}
		byte[] proBytes = sb.toString().getBytes();
		byte[] b = new byte[mac.length + proBytes.length];
		System.arraycopy(mac, 0, b, 0, mac.length);
		System.arraycopy(proBytes, 0, b, mac.length, proBytes.length);
		return Md5.mD5(Base64.byteArrayToAltBase64(Aes128.encrypt(b, key.getBytes())));

	}

	public static String encrypt(String str) {
		try {
			byte[] encryptByte = Aes128.encrypt(str.getBytes(), key.getBytes());
			return Base64.byteArrayToBase64(encryptByte);
		} catch (Exception e) {
			RunLogger.getLogger().log(Level.SEVERE, e.getMessage(), e);
			return str;
		}
	}

	public static String decrypt(String str) {
		try {
			return new String(Aes128.decrypt(Base64.decode(str), key.getBytes()));
		} catch (Exception e) {
			RunLogger.getLogger().log(Level.SEVERE, e.getMessage(), e);
			return str;
		}
	}

	public static String generateRegistrationCode(String... cfgStr) throws Exception {
		return generateRegistrationCode(getHardwareAddress().getBytes(), cfgStr);
	}

	public static byte[] getMacAddress() {
		InetAddress inetAddr;
		try {
			inetAddr = InetAddress.getLocalHost();
			NetworkInterface netInt = NetworkInterface.getByInetAddress(inetAddr);
			byte[] array = netInt.getHardwareAddress();
			if (array != null) {
				return array;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new byte[] { 127, 0, 0, 1 };
	}

	/**
	 * 获取环境的物理地址
	 *
	 * @return
	 */
	public static String getHardwareAddress() {
		byte[] array = getMacAddress();
		StringBuffer sb = new StringBuffer();
		if (array.length > 0) {
			sb.append(StringUtils.toHex(array[0]).toUpperCase());
			for (int i = 1; i < array.length; i++) {
				sb.append("-").append(StringUtils.toHex(array[i]).toUpperCase());
			}
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		InetAddress inetAddr;
		try {
			inetAddr = InetAddress.getLocalHost();
			NetworkInterface netInt = NetworkInterface.getByInetAddress(inetAddr);
			byte[] array = netInt.getHardwareAddress();
			StringBuffer sb = new StringBuffer();
			if (array.length > 0) {
				sb.append(StringUtils.toHex(array[0]).toUpperCase());
				for (int i = 1; i < array.length; i++) {
					sb.append("-").append(StringUtils.toHex(array[i]).toUpperCase());
				}
			}
			System.out.println(inetAddr);
			System.out.println(netInt);
			System.out.println(sb);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
